//
//  AppDelegate+AppServices.m
//  ZPFHimalayan
//
//  Created by zhou on 2018/11/23.
//  Copyright © 2018 MissZhou. All rights reserved.
//

#import "AppDelegate+AppServices.h"
#import "OpenUDID.h"
#import "RootNavigationViewController.h"
#import "ZPFLoginViewController.h"
#import <AFNetworkActivityIndicatorManager.h>

@implementation AppDelegate (AppServices)

#pragma mark - 初始化服务
- (void)initService{
    //注册登录状态监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didLoginStateChangeAction:)
                                                 name:KNotificationLoginStateChange
                                               object:nil];
    //注册网络状态监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didNetworkStateChangeAction:) name:KNotificationNetworkStateChange
                                               object:nil];
}

#pragma mark - 初始化window
- (void)initWindow{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [[UIButton appearance] setExclusiveTouch:YES];
    [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = [UIColor whiteColor];
    if (@available(iOS 11.0, *)) {
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }
}

#pragma mark - 初始化用户系统
- (void)initUserManager{
//    DLog(@"设备IEMI：%@",[OpenUDID value]);
    if ([ZPFUserManager sharedZPFUserManager].currentUserInfo) {
        //如果本地数据存在，那么先展示Tabbar，随后异步自定登录
        self.mainTabBar = [[MainTabBarViewController alloc] init];
        self.window.rootViewController = self.mainTabBar;
        
        //自动登录
        [[ZPFUserManager sharedZPFUserManager] autoLoginToService:^(BOOL success, NSString *message) {
            if (success) {
                DLog(@"自动登录成功");
                kPostNotification(KNotificationLoginStateChange, nil);
            }else{
                DLog(@"自动登录失败:%@",message);
            }
        }];
    }else{
        //未登录过，展示登录页面
        kPostNotification(KNotificationLoginStateChange, @NO);
    }
}

#pragma mark - 登录状态处理
- (void)didLoginStateChangeAction:(NSNotification *)notification{
    BOOL loginSuccessState = [notification.object boolValue];
    if (loginSuccessState) {
        //登录成功加载主窗口控制器
        //为了避免自动登录重复加载tabBar
        if (!self.mainTabBar || ![self.window.rootViewController isKindOfClass:[MainTabBarViewController class]]) {
            self.mainTabBar = [[MainTabBarViewController alloc] init];
            CATransition *anima = [CATransition animation];
            //设置动画类型
            anima.type = @"cube";
            //设置动画方向
            anima.subtype = kCATransitionFromRight;
            anima.duration = 0.5;
            self.window.rootViewController = self.mainTabBar;
            [kAppWindow.layer addAnimation:anima forKey:@"revealAnimation"];
        }
    }else{
        //登录失败加载登录页面控制器
        self.mainTabBar = nil;
        RootNavigationViewController *rootNav = [[RootNavigationViewController alloc] initWithRootViewController:[ZPFLoginViewController new]];
        CATransition *anima = [CATransition animation];
        //设置动画类型
        anima.type = @"cube";
        //设置动画方向
        anima.subtype = kCATransitionFromRight;
        anima.duration = 0.5;
        self.window.rootViewController = rootNav;
        [kAppWindow.layer addAnimation:anima forKey:@"revealAnimation"];
    }
    //展示FPS
    [ZPFAppManager showFPS];
}

#pragma mark - 网络状态变化
- (void)didNetworkStateChangeAction:(NSNotification *)notification{
    int networkState = [[notification object] intValue];
    switch (networkState) {
        case -1:
        {
            //未知网络状态
        }
            break;
            
        case 0:
        {
            //没有网络
            [MFHUDManager showError:@"网络跑路了..."];
        }
            break;
            
        case 1:
        {
            //3G或者4G
            [MFHUDManager showWarning:@"当前为4G网络"];
        }
            break;
            
        case 2:
        {
            //WIFI网络
            [MFHUDManager showWarning:@"当前为WIFI网络"];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 检测网络状态变化
-(void)monitorNetworkStatus
{
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
    self.netWorkStatesCode =AFNetworkReachabilityStatusUnknown;
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        self.netWorkStatesCode = status;
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"当前使用的是4G流量模式");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"当前使用的是wifi模式");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"断网了");
                break;
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"变成了未知网络状态");
                break;
                
            default:
                break;
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationNetworkStateChange object:@(status)];
    }];
    [manager.reachabilityManager startMonitoring];
}

#pragma mark - 初始化MFHUD
- (void)initMFHUD{
    //    [MFHUDManager setHUDType:MFHUDTypeLarge];
    [MFHUDManager setHUDType:MFHUDTypeNormal];  //-- 默认
    [MFHUDManager setMaskType:MFHUDMaskTypeDark];
    //[MFHUDManager setMaskType:MFHUDMaskTypeLight]; --默认
}

#pragma mark - 设置键盘回滚
- (void)initKeyBoard{
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    
}

+ (AppDelegate *)shareAppDelegate{
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}
@end
